package com.yc.myspringboot.service.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class TestLock {
	
	@Autowired
	private StringRedisTemplate redisTemplate;
	
	private static Integer account = 0;
	
//	public static void main(String[] args) {
//		TestLock tl = new TestLock();
//		tl.m();
//		System.out.println("account =" + account);
//	}

	/**
	 * 起三个线程 同时对一个账号进行修改
	 */
	public Integer m() {
		account = 0;
		Thread t1 = new Thread(new handler());
		Thread t2 = new Thread(new handler());
		t1.start();
		t2.start();
		try {
			t1.join();
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return account;
	}
	
	//普通线程
	class handler implements Runnable {
		@Override
		public void run() {
			for (int i = 0; i < 5000; i++) {
				account++;
			}
			
		}
	}
	
	//增加redis锁的线程
//	class handler implements Runnable {
//		@Override
//		public void run() {
//			for (int i = 0; i < 5000; i++) {
//				account++;
//			}
//			
//		}
//	}
}
